//
//  YQLocationVC.m
//  MuYing
//
//  Created by tbb on 2019/8/21.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "YQLocationVC.h"
#import "YQSearchBaseBar.h"
#import "YQNavTitleView.h"
#import "YJLocationTool.h"
#define categoryCollViewItemSizeW 88
#define categoryCollViewItemSizeH 47

@interface YQLocationVC ()<YQSearchBaseBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource,YQLocationShowCityCellDelegate>
@property (nonatomic, strong) UIButton * cancelBtn;
@property (nonatomic, strong) YQSearchBaseBar * searchBar;
@property (nonatomic, strong) YQLocationTopView * topView;

@property (nonatomic, strong) UICollectionView * categoryCollView;

@property (nonatomic, strong) YQBaseTableView * showTableView;
@property (nonatomic , strong) YJLocationTool *locationTool;

@property (nonatomic, copy) NSString * city;
@end

@implementation YQLocationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

-(void)setupView {
    [super setupView];
    YQNavTitleView *navView = [YQNavTitleView new];
    navView.frame = CGRectMake(0, 0, kScreenWidth, 44);
    navView.intrinsicContentSize = CGSizeMake(kScreenWidth, 44);
    self.navigationItem.titleView = navView;
    
    UIView *bgView = [UIView new];
    //    bgView.backgroundColor = UIColor.yellowColor;
    [navView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 44));
    }];
    
    [bgView addSubview:self.searchBar];
    
    [bgView addSubview:self.cancelBtn];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.searchBar.mas_centerY).mas_equalTo(0);
        make.right.mas_equalTo(-12);
        make.height.mas_equalTo(20);
    }];
    
    [self.view addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    
    [self.view addSubview:self.categoryCollView];
    [self.view addSubview:self.showTableView];
    
    NSString *userCityId = [Config currentConfig].city;
    if ([userCityId isEqualToString:@"<null>"] || userCityId.length == 0 ) {
        if (![self.locationTool startLocation]) {
            //设置默认地区
            self.city = @"深圳";
            [self.topView.locationBtn setTitle:@"深圳" forState:(UIControlStateNormal)];
        }else{
            ws(self);
            self.locationTool.areaNameBlock = ^(NSString *provinceName, NSString *cityName, NSString *districtName, NSError *err) {
                
                if ([cityName removeStringSpace].length == 0) {
                    return;
                }
                //[weakself.localeBtn setTitle:cityName forState:(UIControlStateNormal)];
                weakself.city = cityName;
                if (!provinceName) {
                   [weakself.topView.locationBtn setTitle:cityName forState:(UIControlStateNormal)];
                }else{
                    [weakself.topView.locationBtn setTitle:[provinceName stringByAppendingString:cityName] forState:(UIControlStateNormal)];
                }
                
            };
        }
    }
    
}

#pragma mark -- <UITableViewDelegate,UITableViewDataSource>
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 15;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YQLocationShowCityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YQLocationShowCityCell"];
    cell.selectIndexPath = indexPath;
    cell.delegate = self;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 72;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
#pragma mark -- <UITableViewDelegate,UITableViewDataSource> end

#pragma mark -- <UICollectionViewDelegate,UICollectionViewDataSource>
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 15;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YQLocationCategoryCollViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YQLocationCategoryCollViewCell" forIndexPath:indexPath];
    cell.titleLb.text = kYQLS(@"为你推荐");
    return  cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    YQLocationCategoryCollViewCell *cell = (YQLocationCategoryCollViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    NSLog(@"CellFrame: %@",NSStringFromCGRect(cell.frame));
    cell.isSelectCell = YES;
    
    NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.item];
    //滚动到其相应的位置
    [self.showTableView scrollToRowAtIndexPath:scrollIndexPath
                            atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    YQLocationCategoryCollViewCell *cell = (YQLocationCategoryCollViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.isSelectCell = NO;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) {
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    YQLocationHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"YQLocationHeader"];
    header.titleLb.text = @"广东";
    return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 83;
}
#pragma mark -- <UICollectionViewDelegate,UICollectionViewDataSource> end

#pragma makr -- lazy
-(YQBaseTableView *)showTableView {
    if (!_showTableView) {
        _showTableView = [[YQBaseTableView alloc] initWithFrame:CGRectMake(categoryCollViewItemSizeW, 44, kYQPScreenWidth - categoryCollViewItemSizeW, kYQPScreenHeight - 44 - Height_NavBar - kHeight_Botton_SafeArea) style:UITableViewStyleGrouped];
        _showTableView.backgroundColor = HexColor(0xF6F6F6);
        _showTableView.dataSource = self;
        _showTableView.delegate = self;
        [_showTableView registerNib:[UINib nibWithNibName:@"YQLocationHeader" bundle:nil] forHeaderFooterViewReuseIdentifier:@"YQLocationHeader"];
        [_showTableView registerClass:[YQLocationShowCityCell class] forCellReuseIdentifier:@"YQLocationShowCityCell"];
    }
    return _showTableView;
}

-(UICollectionView *)categoryCollView {
    if (!_categoryCollView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 2;
        layout.minimumInteritemSpacing = 0.01;
        layout.itemSize = CGSizeMake(categoryCollViewItemSizeW, categoryCollViewItemSizeH);
        _categoryCollView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 44, categoryCollViewItemSizeW, kYQPScreenHeight - 44 - Height_NavBar - kHeight_Botton_SafeArea) collectionViewLayout:layout];
        _categoryCollView.backgroundColor = HexColor(0xF6F6F6);
        _categoryCollView.delegate = self;
        _categoryCollView.dataSource = self;
        _categoryCollView.showsVerticalScrollIndicator = NO;
        [_categoryCollView registerNib:[UINib nibWithNibName:@"YQLocationCategoryCollViewCell" bundle:nil] forCellWithReuseIdentifier:@"YQLocationCategoryCollViewCell"];
    }
    return _categoryCollView;
}

-(YQLocationTopView *)topView {
    if (!_topView) {
        _topView = [YQLocationTopView viewFromXib];
        _topView.backgroundColor = UIColor.whiteColor;
        [_topView.locationBtn setTitle:@"中国 广东 深圳" forState:(UIControlStateNormal)];
        [_topView.withLocationBtn setTitle:@"使用当前定位" forState:(UIControlStateNormal)];
        [_topView.withLocationBtn addTapAction:^(id  _Nonnull sender) {
            if (self.selectCityBlack) {
                self.selectCityBlack(self.city);
                [self.navigationController dismissViewControllerAnimated:true completion:nil];
            }
        }];
    }
    return _topView;
}
-(YQSearchBaseBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [[YQSearchBaseBar alloc] initWithFrame:CGRectMake(12, 0, SCREEN_WIDTH - 68, 28)];
        _searchBar.top = 11;
        _searchBar.delegate = self;
        _searchBar.placeholderStr = kYQLS(@"搜索您所需要的地理位置");
    }
    return _searchBar;
}

-(UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:0];
        _cancelBtn.titleLabel.font = kSystemMedium(14);
        [_cancelBtn setTitleColor:HexColor(0xFA969D) forState:(UIControlStateNormal)];
        [_cancelBtn setTitle:kYQLS(@"取消") forState:(UIControlStateNormal)];
        ws(self);
        [_cancelBtn addTapAction:^(id  _Nonnull sender) {
            [weakself.navigationController dismissViewControllerAnimated:true completion:nil];
        }];
    }
    return _cancelBtn;
}

- (YJLocationTool *)locationTool
{
    if (!_locationTool) {
        _locationTool = [[YJLocationTool alloc] init];
        _locationTool.blockType = YJLocationBlockTypeAreaName;
    }
    return _locationTool;
}

#pragma makr -- lazy end

#pragma makr -- YQSearchBaseBarDelegate
- (void)yq_searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    
}
-(void)yq_textFieldShouldClear:(UITextField *)textField {
    
}
-(void)yq_shouldChangeText:(NSString *)text {
    
}
#pragma makr -- YQSearchBaseBarDelegate end

#pragma mark -- YQLocationShowCityCellDelegate
- (void)didSelectItemAtImageAction:(NSIndexPath *)indexPath :(NSInteger)item {
    NSArray *arr = @[@"广州",@"深圳",@"汕头",@"湛江",@"茂名",@"汕尾"];
    if (self.selectCityBlack) {
        self.selectCityBlack(arr[item]);
        [self.navigationController dismissViewControllerAnimated:true completion:nil];
    }
}
#pragma mark -- YQLocationShowCityCellDelegate end
@end
